//
//  GTSettingCell.m
//  GiftTalk
//
//  Created by Wan on 16/4/17.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTSettingCell.h"
#import "GTSettingItem.h"
#import "GTSettingArrowItem.h"
#import "GTSettingSwitchItem.h"
#import "GTSettingLabelItem.h"

@interface GTSettingCell ()
/** cell右侧视图 Label */
@property (strong, nonatomic) UILabel *labelView;
/** cell右边视图 Switch 按钮 */
@property (strong, nonatomic) UISwitch *switchView;
/** cell右边视图 箭头图标 */
@property (strong, nonatomic) UIImageView *imgView;

@end

@implementation GTSettingCell

- (UILabel *)labelView {
    if (!_labelView) {
        _labelView = [[UILabel alloc] init];
        _labelView.bounds = CGRectMake(0, 0, 100, 44);
        _labelView.textColor = [UIColor grayColor];
        _labelView.textAlignment = NSTextAlignmentRight;
    }
    return  _labelView;
}

- (UISwitch *)switchView {
    if (!_switchView) {
        _switchView = [[UISwitch alloc] init];
    }
    return _switchView;
}

- (UIImageView *)imgView {
    if (!_imgView) {
//        [UIImageView alloc] initWithImage:<#(nullable UIImage *)#>]
        _imgView = [[UIImageView alloc] init];
    }
    return _imgView;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    //1. 创建可重用 cell
    static NSString * reuseIdentifier = @"settingCell";
    GTSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    //2. 缓存池中没有，则重新创建 唯一标识符为cellId的cell单元格
    if (!cell) {
        // 我的身份 - 需要子标题，故更改默认的cell样式为 带子标题的UITableViewCellStyleValue1
        cell = [[GTSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

# pragma mark - Lift Cycle

- (void)awakeFromNib {
    
}

#pragma mark - setter

- (void)setItem:(GTSettingItem *)item {
    _item = item;
    
    // 设置 cell 的子控件数据
    [self setupCellForRowData];
    
    // 设置 cel 右边视图或图标类型
    [self setupAccessoryViewOrType];
}

#pragma mark - Other Function

- (void)setupCellForRowData {
    // 判断 cell 的图片是否为空
    if (self.item.icon.length) {
        self.imageView.image = [UIImage imageNamed:self.item.icon];
    }
    self.textLabel.text = self.item.title;
    
    self.detailTextLabel.text = self.item.subTitle;
}

- (void)setupAccessoryViewOrType {
    if ([self.item isKindOfClass:[GTSettingArrowItem class]]) { // 右边带箭头
        self.accessoryView = self.imgView;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    } else if ([self.item isKindOfClass:[GTSettingSwitchItem class]]) {
        self.accessoryView = self.switchView;
        // 右侧为UISwitch按钮则去除行选中的样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    } else if ([self.item isKindOfClass:[GTSettingLabelItem class]]) {
        self.accessoryView = self.labelView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 强转 获取自定义label 的text属性
        GTSettingLabelItem *labelItem = (GTSettingLabelItem *)self.item.title;
        self.labelView.text = labelItem.text;
        
    } else {
        self.accessoryView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
}

@end
